package weaver.jobs.ztj;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-08-17
 * @Description: 定时任务推送发票信息到ERP
 * @ModifyLog:
 **/

public class WeapushInvoiceCronJob extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            String fullDate = DateUtil.getFullDate();
            baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>fullDate=" + fullDate);
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id,invoicenumber,invoicecode,invoicetype,billingdate,seller,taxincludedprice,entryTime," +
                    "pricewithouttax,userid_new,billingdate from fnainvoiceledger where ifnull(ciphertext,0)=0");
            while (rs.next()) {
                JSONObject paramJson = new JSONObject();
                JSONArray detailList = new JSONArray();
                JSONObject detailJson = new JSONObject();
                String id = Util.null2String(rs.getString("id"));
                String seller = Util.null2String(rs.getString("seller"));
                String invoicetype = Util.null2String(rs.getString("invoicetype"));
                baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>seller=" + seller+",invoicetype="+invoicetype);
                String csbh = getGysbm(seller);
                baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>seller=" + seller+",csbh="+csbh);
                if (!"".equals(id) && !"".equals(csbh)) {
                    detailJson.put("wbid", id);
                    detailJson.put("operate", "1");
                    detailJson.put("fphm", Util.null2String(rs.getString("invoicenumber")));
                    detailJson.put("fplb", Util.null2String(rs.getString("invoicecode")));
                    detailJson.put("fplx", invoicetype);
                    detailJson.put("kprq", Util.null2String(rs.getString("billingdate")));
                    detailJson.put("csbh", csbh);
                    detailJson.put("fpsl", getFpsl(id));
                    detailJson.put("fpje", Util.null2String(rs.getString("taxincludedprice")));
                    detailJson.put("csje", Util.null2String(rs.getString("pricewithouttax")));
                    detailJson.put("kprm", Util.null2String(rs.getString("userid_new")));
                    detailJson.put("kpsj", Util.null2String(rs.getString("billingdate")));
                    detailJson.put("jzrq", Util.null2String(rs.getString("entryTime")));
                    detailList.add(detailJson);
                    JSONObject dataJson = new JSONObject();
                    dataJson.put("Details", detailList);
                    JSONObject DataJson = new JSONObject();
                    DataJson.put("Data", dataJson);
                    paramJson.put("data", DataJson);
                    baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>paramJson=" + paramJson.toJSONString());
                    String returnStr = sendOKHttp3Request(paramJson.toJSONString());
                    JSONObject returnJson = JSONObject.parseObject(returnStr);
                    baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>returnJson=" + returnJson.toJSONString());
                    String success = Util.null2String(returnJson.getString("success"));
                    if ("true".equals(success)) {
                        boolean flag = rs.execute("update fnainvoiceledger set ciphertext=1 where id=" + id);
                        baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>flag=" + flag);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String sendOKHttp3Request(String paramJson) {
        String result = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = RequestBody.create(mediaType, "");
            String url = "http://58.241.13.10:11896/Api/PutData/ExecProcedureWithReturnJsonError";
            String dataflow = "dataflow=泛微oa→RZERPv4_铁建工科";
            String actionCode = "actionCode=put_gyfpb";

            String data = "data=" + paramJson;
            Request request = new Request.Builder()
                    .url(url + "?" + dataflow + "&" + actionCode + "&" + data)
                    .method("POST", body)
                    .addHeader("Accept", "*/*")
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static String getGysbm(String gysmc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_gystz where gsmc='" + gysmc + "'");
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getFpsl(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select sum(unitnumber2) from fnainvoiceledgerdetail where mainid='" + id + "'");
        rs.next();
        return Util.null2String(rs.getString(1));
    }

}

